package com.heima.article.v1.controller;

import com.heima.article.entity.ApArticle;
import com.heima.article.service.ApArticleService;
import com.heima.common.dtos.ResponseResult;


import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.dtos.ArticleLoadDTO;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import com.heima.user.entity.FollowConstant;
import com.heima.user.v1.controller.UserRelationController;
import com.hiema.controll.VehaviorControll;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/api/v1/article")
public class ArticleController {

    @Autowired
    private ApArticleService articleService;
    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 加载文章行为-数据回显
     * @param dto
     * @return
     */
    @ApiOperation("加载文章行为-数据回显")
    @PostMapping("/load_article_behavior")
    public ResponseResult behavior(@RequestBody ArticleInfoDto dto){
        Integer userId = UserThreadLocalUtils.getUserId();
        log.info("当前登录的用户Id为:{},当前文章为:{}",userId,dto.getArticleId());
        String key=""+dto.getArticleId();
        log.info("当前redis ---------->:{}",key);
/*        redisTemplate.opsForHash().put(key,"hhh","1");
        redisTemplate.opsForHash().put(key,"xxx","1");
        redisTemplate.opsForHash().put(key,"www","1");
        redisTemplate.opsForHash().put(key,"ttt","1");
*/


        //获取点赞的vlaue
        String like = (String)redisTemplate.opsForHash().get(key, VehaviorControll.OPERATION_LIKE);
        //获取点赞的vlaue
//      String unLike = (String) redisTemplate.opsForHash().get(key, VehaviorControll.OPERATION_LIKE);
        //获取收藏的vlaue
        String collection = (String) redisTemplate.opsForHash().get(key,ArticleBehaviorCollection.COLLECT);
        //获取关注的vlaue
        String follow = (String) redisTemplate.opsForHash().get(key,UserRelationController.REDIS_KEY_ISFOLLOW);

        return articleService.judgmentBehavior(collection,like,follow);
        //return articleService.judgmentBehavior(like,unLike,collection,follow);

        //return ResponseResult.ok();
    }



    /**
     * feign, 搜索服务做批量的数据导入
     *
     */
    @GetMapping("/findByPage")
    public List<ArticleDto> findByPage(@RequestParam("page") int page, @RequestParam("size")int size) {
        return articleService.findByPage(page, size);
    }

    /**
     * feign, 搜索服务收到创建文章的消息，调用此方法查询文章的详情
     *
     */
    @GetMapping("/findById")
    public ArticleDto findById(@RequestParam("articleId") long articleId){
        return articleService.findById(articleId);
    }


    /**
     * feign, 当自媒体文章审核通过以后，调用此方法保存文章，保存文章内容
     *
     */
    @PostMapping("/save")
    public Long createArticle(@RequestBody WmNewsResultDTO dto){
        return articleService.createArticle(dto);
    }

    /**
     * app首页显示文章数据
     * */
    @PostMapping("/load")
    public ResponseResult<List<ApArticle>> load(@RequestBody ArticleLoadDTO dto){
        List<ApArticle> list = articleService.load(dto);
        return ResponseResult.ok(list);
    }
    /**
     * 上拖动页面，查询更多文章
     * */
    @PostMapping("/loadmore")
    public ResponseResult<List<ApArticle>> loadmore(@RequestBody ArticleLoadDTO dto){
        List<ApArticle> list = articleService.loadmore(dto);
        return ResponseResult.ok(list);
    }

    /**
     * 下拖动页面，查询最新文章
     * */
    @PostMapping("/loadnew")
    public ResponseResult<List<ApArticle>> loadnew(@RequestBody ArticleLoadDTO dto){
        List<ApArticle> list = articleService.loadnew(dto);
        return ResponseResult.ok(list);
    }
}
